/**
 * @Description: 自定义模板模块
 * @author longfa liang
 * @date 2021/7/13
 */
var pu = require('../tool/public')
var mg = require('../db/userdb')
var pr_mg = require('../db/processdb')

class template {

    /*查询表单*/
    async find_template (req, res, next) {
        let team_id = req.headers.team
        let status = Number(req.body.status)
        let release = Number(req.body.release)
        let modeule = req.body.modeule
        let template_id = req.body.template_id
        let userid = req.body.userid
        let sort = req.body.sort
        let text = req.body.text
        let _id = req.body._id
        let uuid = req.body.uuid
        let where = {
            team_id: mg.ObjectID(team_id)
        }
        if (release) {
            where.is_release = release
        }
        if (template_id) {
            where.template_id = mg.ObjectID(template_id)
        }
        if (modeule) {
            where.modeule = modeule
        }
        if (userid) {
            where.userid = mg.ObjectID(userid)
        }
        if (text) {
            where.name = { $regex: text }
        }
        if (status == 1) {
            where.processid = { $exists: false } //没有流程
        }
        if (uuid) {
            where.uuid = uuid
        }
        var limit = ''
        if (sort) {
            limit = 1
        }

        if (_id) {
            where._id = mg.ObjectID(_id)
            let info = await pr_mg.findOne('template', where)
            pu.success('OK', info, res)
        } else {
            let info = await pr_mg.find('template', where, '', '', limit, { version: -1 })
            pu.success('OK', info, res)
        }
    }

    /*修改表单*/
    async update_template (req, res, next) {
        let uuid = req.body.uuid //id
        let template = req.body.template //表单模板
        let status = req.body.status //
        let release = Number(req.body.release) //模板状态 0不发布 1发布
        let team_id = req.headers.team
        let isnull = pu.isnull([uuid, release])
        if (isnull) {
            pu.fail('参数不正确', res)
            return
        }
        let data = await pr_mg.find('template', { team_id: pr_mg.ObjectID(team_id), uuid: uuid }, '', '', 1, { version: -1 })
        if (!!data.length) {
            pu.fail('_id不正确', res)
            return
        }
        data = data[0]
        if (template) {
            data.template = template
        }
        if (status || status == 0) {
            data.status = status
        }
        if (data.release == 0 && release == 0) {//0保存
            await pr_mg.updateOne('template', { _id: data._id }, { $set: data })
        } else {
            delete data._id
            data.version = data.version + 1
            await pr_mg.insertOne('template', data)
        }
        pu.success('OK', data, res)
    }

    /*新增表单*/


    async add_template (req, res, next) {
        let team_id = req.headers.team
        let name = req.body.name //名称
        let template = req.body.template //表单模板
        let uid = req.headers.uid //用户id
        let modeule = req.body.modeule //所属模板
        let modeule_url = req.body.modeule_url //所属模板
        let explain = req.body.explain //说明
        let modeuleName = req.body.modeuleName //模板名称
        let release = Number(req.body.release) //模板状态 0不发布 1发布
        let isnull = pu.isnull([name, template, uid, modeule, modeuleName, modeule_url])
        if (isnull) {
            pu.fail('参数不正确', res)
            return
        }
        if (typeof template == 'string') {
            try {
                template = JSON.parse(template)
            } catch (e) {
                console.log(e)
                pu.fail('参数不正确', res)
                return
            }
        }
        let insetdata = {
            name: name,
            team_id: mg.ObjectID(team_id),
            uid: mg.ObjectID(uid),
            template: template,
            modeule: modeule,
            startTime: new Date().getTime(),
            modeuleName: modeuleName,
            modeule_url: modeule_url,
            version: 1,
            is_delete: 0,
            uuid: pu.uuid(),
            is_release: release,//是否发布
            explain: explain//说明
        }
        let temres = await pr_mg.insertOne('template', insetdata)
        if (temres.insertedId) {
            insetdata._id = temres.insertedId
            pu.success('OK', insetdata, res)
        } else {
            pu.fail('数据格式不正确', res)
            return
        }
    }
}

module.exports = new template()
